<style>
	.username{cursor: text;}
	.header_alt .username:hover{color:#fff;}
	.logout{cursor: pointer;}
</style>
<template>
	<!-- <div class="header"> -->
		<el-header>
			<div class="header_index">
	  			<div class="header_logo">
	  				<img src="../../assets/img/Weblogo.png">
	  			</div>
	  			
	  			<div class="header_nav">
	  				<el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect"  unique-opened router >
						<template v-for="(item,index) in $router.options.routes" v-if="!item.hidden">
							<el-submenu :index="index+''" v-if="!item.leaf">
								<template slot="title"><i :class="item.iconCls"></i>{{item.name}}</template>
								<el-menu-item v-for="child in item.children" :index="index+''" :route="child" :key="child.path" v-if="!child.hidden">{{child.name}}</el-menu-item>
							</el-submenu>
					
							<el-menu-item :route="item" v-if="item.leaf" :index="index+''">{{item.name}}</el-menu-item>
						</template>
						<div class="el-menu-item">
							<a href="./static/help.pdf">新手帮助</a>
						</div>

					</el-menu>

	  			</div>
	  		</div>
	  		<div v-if="!userId" class="header_alt">		  				
				<router-link to="/login">登录</router-link>
				<router-link to="/register">注册</router-link>
	  		</div>	
	  		<div v-if="userId" class="header_alt">		  				
				<a class="username"><span>{{userInfo.userName}}</span></a>
				<a class="logout"><span @click="logout">退出</span></a>
	  		</div>	
  		</el-header>
	<!-- </div> -->
</template>
<script>
import * as users from '../../api/users';
import cookie from '../../api/cookie.js'//当前cookie

	export default{
		data(){
			return{
			   activeIndex:'',
			   userId:'',
			   userName:'',
			   userInfo:'',

			}
		},
		methods:{
			logout(){
				 cookie.logout('merchant_token');
               	sessionStorage.setItem("userId",'')
               	this.$router.push('/login');
			},
			handleSelect(key, keyPath) {
	       	 	console.log(key, keyPath);
				 // this.activeIndex = key+'';
				 this.$set(this,'activeIndex',key+'')
				 sessionStorage.setItem('activeIndex',key+'');
	      	},
      	   fetchDataUser(){
		    	//获取用户信息
		    	let para={
		    		userId:this.userId,
		    	}
			  	users.info.r(para).then((res) => {
			  		console.log(res)
			  		if(res.data.retCode==1){
			  			if(res.data.data.realName){
		  					this.isRealName=true;
		  				}else{
		  					this.isRealName=false;
		  				}
		  				this.userInfo=res.data.data;
		  				sessionStorage.setItem("localUser",JSON.stringify(res.data.data))
			  		}else{
			  			this.$message.error(res.data.retMsg);
			  		}
			       
			       

			      });
		    },
	     
		},
		created(){
    		this.userId=sessionStorage.getItem('userId')
    		if(this.userId){
    			this.fetchDataUser();

    		}

			 // this.activeIndex=sessionStorage.getItem('activeIndex')?sessionStorage.getItem('activeIndex'):sessionStorage.getItem('activeIndex')!=''?'': '0';
			 this.activeIndex=sessionStorage.getItem('activeIndex')=="login"?'':sessionStorage.getItem('activeIndex')?sessionStorage.getItem('activeIndex'): '0';
			 console.log(this.activeIndex)
		}
	}
</script>